import ajax from '../../libs/ajax.js';
import tools from '../../libs/tools.js';

//#region 处理好友信息
let ym = document.getElementById('ym');
let cs1 = {
  info: ''
};
let page = {
  pageNumber: 1,
  pageSize: 5
};
let sz = [];
function clhyxx() {
  ajax.send('/user/auth/friendQuery', tools.concatJson(cs1, page), (data) => {
    if (data.success) {
      sz = data.list;
      page = data.page;
      zs();
    }
  });
}

let datalist = document.getElementById('datalist');
function zs() {
  console.log('dsfsfsfsfsfsfs');
  datalist.innerHTML = '';
  for (let i = 0; i < sz.length; i++) {
    const x = sz[i];
    console.log('内容', x);
    let div1 = document.createElement('div');
    div1.classList.add('item');
    //图片
    let img1 = document.createElement('img');
    img1.classList.add('face');
    if (x.userInfo.img) {
      img1.setAttribute('src', x.userInfo.img);
    } else {
      img1.setAttribute(
        'src',
        'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202005%2F30%2F20200530112650_4XXME.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1628654333&t=ccef3bc418154fea7edd5c5a5902766f'
      );
    }
    div1.append(img1);
    datalist.append(div1);

    //登入名
    let div2 = document.createElement('div');
    div2.classList.add('des');
    let div3 = document.createElement('div');
    div3.classList.add('nickName');
    let span1 = document.createElement('span');
    span1.classList.add('denglu');
    span1.append(x.user.username);
    div3.append(span1);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);

    //账号
    let span2 = document.createElement('span');
    span2.classList.add('denglu');
    span2.append(x.user.nickname);
    div3.append(span2);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);

    
//操作按钮
    let span3 = document.createElement('span');
    span3.classList.add('btn')
    let button1=document.createElement('button1');
    button1.append('同意');
    span3.append(button1);
    div3.append(span3);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);

    let span4 = document.createElement('span');
    span4.classList.add('btn')
    let button2=document.createElement('button2');
    button2.append('拒绝');
    span4.append(button2);
    div3.append(span4);
    div2.append(div3);
    div1.append(div2);
    datalist.append(div1);
    button1.addEventListener('click', () => {
      let cs = {
        info: '',
        username: x.user.username
      };
      ajax.send('/user/auth/friendAgree', cs, (data) => {
        if (data.message) {
          showToast(data.message);
          clhyxx()
        }
      });
    });


    button2.addEventListener('click', () => {
      let cs = {
        info: 'deny',
        username: x.user.username
      };
      ajax.send('/user/auth/friendAgree', cs, (data) => {
        if (data.message) {
          showToast(data.message);
          clhyxx()
        }
      });
    });

    //操作按钮
    // td = document.createElement('td');
    // let an = document.createElement('button');
    // an.append('同意');
    // an.addEventListener('click', () => {
    //   let cs = {
    //     info: '',
    //     username: x.user.username
    //   };
    //   ajax.send('/user/auth/friendAgree', cs, (data) => {
    //     if (data.message) {
    //       showToast(data.message);
    //     }
    //   });
    // });
    // td.append(an);
    // tr.append(td);

    // td = document.createElement('td');
    // let an1 = document.createElement('button');
    // an1.append('拒绝');
    // an1.addEventListener('click', () => {
    //   let cs = {
    //     info: 'deny',
    //     username: x.user.username
    //   };
    //   ajax.send('/user/auth/friendAgree', cs, (data) => {
    //     if (data.message) {
    //       showToast(data.message);
    //     }
    //   });
    // });
    // td.append(an1);
    // tr.append(td);
  }
}

//#endregion

//#region 公用的轻提示对话框

let liveToast = document.getElementById('liveToast');
let liveToastMessing = document.querySelector('#liveToast .toast-body');

const toast = new bootstrap.Toast(liveToast);

function showToast(message) {
  liveToastMessing.innerHTML = message;
  toast.show();
}
//#endregion

clhyxx();
